<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>管理员首页</title>
    <link rel="stylesheet" th:href="@{/webjars/layui/2.5.7/css/layui.css}">
</head>
<body>
<div class="layui-layout layui-layout-admin">

    <div th:replace="layout::header"></div>
    <div th:replace="layout::side"></div>

<div class="layui-body">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
        <legend>常规使用：普通图片上传</legend>
    </fieldset>
    <div class="layui-upload">
        <button type="button" class="layui-btn" id="test1">上传图片</button>
        <div class="layui-upload-list">
            <img class="layui-upload-img" id="demo1">
            <p id="demoText"></p>
        </div>
    </div>


    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
        <legend>指定允许上传的文件类型</legend>
    </fieldset>
    <button type="button" class="layui-btn" id="test3"><i class="layui-icon"></i>上传文件</button>
    <button type="button" class="layui-btn layui-btn-primary" id="test4"><i class="layui-icon"></i>只允许压缩文件</button>
    <button type="button" class="layui-btn" id="test5"><i class="layui-icon"></i>上传视频</button>
    <button type="button" class="layui-btn" id="test6"><i class="layui-icon"></i>上传音频</button>
    <div style="margin-top: 10px;">

    </div>


    <div class="layui-footer">
        <!-- 底部固定区域 -->
        底部固定区域
    </div>

</div>

<script th:src="@{/webjars/layui/2.5.7/layui.js}"></script>
    <script>
        layui.use('upload', function(){
            var upload = layui.upload;
            //执行实例
            var uploadInst = upload.render({
                elem: '#test1' //绑定元素
                ,url: '/admin/fileUpload/' //上传接口
                ,before: function (obj){
                    layer.msg('上传中', {
                        icon: 16,
                        shade: 0.01
                    }); //上传loading
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result){
                        $('#demo1').attr('src', result); //图片链接（base64）
                    });
                }
                ,done: function(res){
                    //上传完毕回调
                    if(res.code = 200){
                        $('#demo1').attr('src', res.data); //图片链接（base64）
                        return layer.msg('上传成功');
                    }
                    else{
                        return alert("上传失败")
                    }
                }
                ,error: function(){
                    //请求异常回调
                }
            });
        });
    </script>
    <script>
        //JavaScript代码区域
        layui.use('element', function(){
            var element = layui.element;

        });
    </script>
</body>
</html>